package com.system.healthproject.model.admin.service.impl;

import com.system.healthproject.bean.BannerTypeName;
import com.system.healthproject.bean.ResultBanner;
import com.system.healthproject.mapper.BannerMapper;
import com.system.healthproject.mapper.BannerTypeMapper;
import com.system.healthproject.model.admin.service.BannerService;
import com.system.healthproject.pojo.Banner;
import com.system.healthproject.pojo.BannerType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private BannerTypeMapper bannerTypeMapper;

    /**
     * 查询banner类型ID和名称
     * @return
     */
    @Override
    public List<BannerType> selectBannerTypeIdName() {
        return bannerTypeMapper.selectBannerTypeIdName();
    }

    /**
     * 查询banner图
     * @param bannertypeId   banner图类型ID
     * @param createStartTime   时间范围
     * @param createEndTime  时间范围
     * @param currentPage
     * @param limit
     * @return
     */
    @Override
    public List<ResultBanner> selectBanner(Integer bannertypeId, String createStartTime, String createEndTime, Integer currentPage, Integer limit) {
        return bannerMapper.selectBanner(bannertypeId,strDate(createStartTime),strDate(createEndTime),(currentPage-1)*limit,limit);
    }

    /**
     * 统计数据条数
     * @param bannertypeId
     * @param createStartTime
     * @param createEndTime
     * @return
     */
    @Override
    public Integer countBanner(Integer bannertypeId, String createStartTime, String createEndTime) {
        return bannerMapper.countBanner(bannertypeId,strDate(createStartTime),strDate(createEndTime));
    }

    /**
     * 新增banner广告
     * @param bannerTypeId
     * @param bannerDisplay
     * @param bannerContent
     * @param bannerH5path
     * @param bannerImg
     * @return
     */
    @Override
    public String addBanner(Integer bannerTypeId, Integer bannerDisplay, String bannerContent, String bannerH5path, String bannerImg) {
        Integer position = bannerMapper.selectMaxPosition();
        return bannerMapper.addBanner(bannerImg,bannerH5path,bannerContent,bannerDisplay,1,bannerTypeId,new Date(),position+1)>0?"成功":"失败";
    }

    /**
     * 根据ID删除banner
     * @param bannerId
     * @return
     */
    @Override
    public String deleteBanner(Integer bannerId) {
        return bannerMapper.deleteByPrimaryKey(bannerId)>0?"成功":"失败";
    }

    /**
     * banner发布、下架
     * @param bannerId
     * @param bannerState
     * @return
     */
    @Override
    public String releaseDismountBanner(Integer bannerId, Integer bannerState) {
        return bannerMapper.releaseDismountBanner(bannerId,bannerState)>0?"成功":"失败";
    }

    /**
     * 修改banner
     * @param bannerId
     * @param bannerTypeId
     * @param bannerDisplay
     * @param bannerContent
     * @param bannerH5path
     * @param bannerImg
     * @return
     */
    @Override
    public String updateBanner(Integer bannerId, Integer bannerTypeId, Integer bannerDisplay,
                               String bannerContent, String bannerH5path, String bannerImg) {
        return bannerMapper.updateBanner(bannerId,bannerTypeId,bannerDisplay,
                bannerContent,bannerH5path,bannerImg)>0?"成功":"失败";
    }

    @Override
    public Banner selectById(Integer bannerId) {
        return bannerMapper.selectByPrimaryKey(bannerId);
    }


    private Date strDate(String strDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=null;
        if(strDate!=null && !strDate.equals("")){
            try {
                date=sdf.parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return  date;
    }
}
